/*
 * Copyright (c) 2011. This file is public under GPLv3 license.
 * For more information about license please refer to http://www.gnu.org/licenses/gpl.html
 */

package com.freetools.watchdog.commands;

import com.freetools.watchdog.engine.UserInformation;
import com.freetools.watchdog.exceptions.CommandErrorException;
import com.freetools.watchdog.exceptions.ParametersErrorException;
import com.freetools.watchdog.model.CommandName;
import com.freetools.watchdog.model.FileSource;
import com.freetools.watchdog.model.IModelHolder;
import com.freetools.watchdog.model.ISource;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Pattern;

/**
 * Date: Feb 15, 2011
 *
 * @author Michael Morozov
 */
@CommandName("file")
public class FileCmd extends Command
{
    private Logger log = Logger.getLogger(FileCmd.class);

    public void execute(final IModelHolder model) throws ParametersErrorException, CommandErrorException
    {
        if (getParameters().size() == 0)
        {
            showSources(model);
            return;
        }
        model.getSources().clear();
        String fileParam = getParameters().get(0);
        log.debug(fileParam);
        File fileMask = new File(fileParam);
        File filePath = fileMask.getParentFile();
        if (filePath == null)
        {
            filePath = new File("./");
        }
        String fileName = fileMask.getName();
        final Pattern pattern = Pattern.compile(fileName);

        filePath.listFiles(new FileFilter()
        {
            public boolean accept(File pathname)
            {
                boolean result = pathname.isFile() && pattern.matcher(pathname.getName()).matches();
                if (result)
                {
                    FileSource fileSource = null;
                    try
                    {
                        fileSource = new FileSource(pathname);
                    } catch (FileNotFoundException e)
                    {
                        UserInformation.info(e.getMessage());
                        log.error(e);
                        return false;
                    }
                    model.getSources().add(fileSource);
                }
                return result;
            }
        });
        Collections.sort(model.getSources(), new Comparator<ISource>()
        {
            public int compare(ISource o1, ISource o2)
            {
                return new Long(((FileSource) o2).getFile().lastModified()).compareTo(((FileSource) o1).getFile().lastModified());
            }
        });

        showSources(model);
    }

    private void showSources(IModelHolder model)
    {
        UserInformation.info(
                String.format("Sources which will be processed <%d>:", model.getSources().size())
        );
        for (ISource source : model.getSources())
        {
            UserInformation.info(source.toString());
        }
    }
}
